package typejump.gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import typejump.Swagger;

/**
 * This is the UIWidget which displays the Swag Meter.
 * @author g
 *
 */
public class SwagWidget implements UIWidget {
	private Swagger meter;
	public SwagWidget(Swagger meter) {
		this.meter = meter;
	}
	
	private int avw = 0;
	private int avh = 0;
	
	private Rectangle bounds = new Rectangle(1, 1, 1, 1);
	
	private Color swagColor = new Color(0XFFD700);
	
	private Font swagFont = new Font("sans-serif", Font.PLAIN, 16);
	private FontMetrics fm;
	
	private int leftSize = 0;
	private int rightSize = 0;
	
	@Override
	public void setAvailableSpace(int width, int height) {
		this.avw = width;
		this.avh = height;
		
		bounds.x = 5;
		bounds.y = 5;
		bounds.width = width - 10;
		bounds.height = 15;
	}
	@Override
	public void paint(Graphics2D g) {
		g.setFont(swagFont);
		if (fm == null) {
			fm = g.getFontMetrics();
			leftSize = fm.stringWidth("Swag ");
			rightSize = fm.stringWidth(" 0000");
			bounds.width = Math.max(bounds.width, fm.getHeight());
		}

		int rectWidth = bounds.width - leftSize - rightSize;
		double percent = (double)meter.getSwag() / meter.getMaxSwag();
		int swagFill = (int)(rectWidth * percent);
		
		
		g.setColor(Color.GRAY);
		g.fillRect(bounds.x+leftSize, bounds.y, rectWidth, bounds.height);
		
		g.setColor(swagColor);
		g.fillRect(bounds.x+leftSize, bounds.y, swagFill, bounds.height);
		
		g.setColor(Color.BLACK);
		g.drawRect(bounds.x+leftSize, bounds.y, rectWidth, bounds.height);
		
		g.drawString("Swag ", bounds.x, bounds.y + fm.getAscent());
		
		String swags = String.valueOf(meter.getSwag());
		g.drawString(swags, bounds.x + bounds.width - fm.stringWidth(swags), bounds.y + fm.getAscent());
	}
	@Override
	public Rectangle getBounds() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public boolean keyEvent(KeyEvent e) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean mouseEvent(MouseEvent e) {
		// TODO Auto-generated method stub
		return false;
	}
}
